package com.example.plan.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.plan.common.PageRequest;
import com.example.plan.common.result.Result;
import com.example.plan.exception.BusinessException;
import com.example.plan.mapper.UserAddressMapper;
import com.example.plan.model.constnant.DroneEnums;
import com.example.plan.model.dto.userAddress.UserAddressDTO;
import com.example.plan.model.vo.UserAddressVO;
import com.example.plan.model.entity.UserAddress;
import com.example.plan.service.UserAddressService;
import com.example.plan.utils.BeanPageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.example.plan.exception.ErrorCode.UPDATE_USER_ADDRESS_ID_IS_NULL;

@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    /**
     * 添加用户地址
     */
    @Override
    public Result<Void> saveUserAddress(UserAddressDTO dto) {
        UserAddress userAddress= BeanUtil.copyProperties(dto, UserAddress.class);
        return baseMapper.insert(userAddress)>0?Result.success("保存成功"):Result.failed("保存失败");
    }

    /**
     * 删除用户地址
     */
    @Override
    public Result<Void> deleteUserAddress(Long addressId) {
        boolean flag=baseMapper.selectById(addressId)==null||baseMapper.deleteById(addressId)>0;
        return flag?Result.success("删除成功"):Result.failed("删除失败");
    }

    /**
     * 修改用户地址
     */
    @Override
    public Result<Void> updateUserAddress(UserAddressDTO dto) {
        if (dto.getAddress_id()==null) {
            throw new BusinessException(UPDATE_USER_ADDRESS_ID_IS_NULL);
        }
        UserAddress userAddress= BeanUtil.copyProperties(dto, UserAddress.class);
        return baseMapper.updateById(userAddress)>0?Result.success("修改成功"):Result.failed("修改失败");
    }

    /**
     * 分页查询用户地址信息
     * @param pageRequest 分页参数
     * @return Result
     */
    @Override
    public Result<IPage<UserAddressVO>> selectUserAddress(PageRequest pageRequest) {
        QueryWrapper<UserAddress> queryWrapper=new QueryWrapper<>();
        if (pageRequest.getSortOrder()== DroneEnums.sortOrderEnum.ASC.getCode()){
            queryWrapper.orderByAsc(StrUtil.isNotBlank(pageRequest.getSortField()),pageRequest.getSortField());
        }else {
            queryWrapper.orderByDesc(StrUtil.isNotBlank(pageRequest.getSortField()),pageRequest.getSortField());
        }
        Page<UserAddress> page = new Page<>(pageRequest.getCurrent(), pageRequest.getPageSize());
        Page<UserAddress> User = baseMapper.selectPage(page, queryWrapper);

        Page<UserAddressVO> pagevo = BeanPageUtil.copyBean(User, UserAddressVO.class);
        return Result.success(pagevo);

    }


}